<?php
namespace Home\Controller;
/**
 * 商品列表
 * User: linkwhy
 * Date: 2015/10/26
 * Time: 10:49
 */
class GoodsListController extends HomeController{

    private $mode = '';

    public function _initialize(){
        $this->mode = M('goods');
    }

    public function index(){
        $list = $this->mode->field('id,name,picture,price,is_ppl')->where(array('is_sj' => 1, 'class_id' => I('get.tid')))->select();
        $this->assign('list', $list);
        $this->display();
    }

    public function goods_detail(){
        $goods_id = I('get.id');
        //获取用户的测量尺寸设置
        $data = M('clients')->field('foot_width,foot_long,foot_thick')->where(array('uid' => $_SESSION['userid']))->find();
        //图片集
        $pdata = M('photos')->field('imgs')->where(array('gid' => $goods_id))->find();
        //颜色
        $cdata = M('color')->field('id,name,images')->where(array('gid' => $goods_id))->find();
        $clist_1 = explode(',', $cdata['name']);
        $clist_2 = explode(',', $cdata['images']);
        $clist = array();
        foreach($clist_1 as $key => $val){
            $clist[$key]['name'] = $val;
            if(!empty($clist_2[$key])){
                $clist[$key]['img'] = getImgs($clist_2[$key], 0);
            }else{
                $clist[$key]['img'] = '';
            }
        }
        //尺寸
        $sdata  = M('size')->field('id,name,num')->where(array('gid' => $goods_id))->find();
        $slist_1 = explode(',', $sdata['name']);
        $slist_2 = explode(',', $sdata['num']);
        $slist = array();
        foreach($slist_1 as $key => $val){
            $slist[$key]['id'] = $sdata['id'];
            $slist[$key]['name'] = $val;
            if(!empty($slist_2[$key])){
                $slist[$key]['num'] = intval($slist_2[$key]);
            }else{
                $slist[$key]['num'] = 0;
            }
        }
        $data['name'] = '商品详情';
        //商品属性
        $info = M('goods_introduce')->field(true)->where(array('gid' => $goods_id))->find();
        $row = $this->mode->field(true)->where(array('id' => $goods_id))->find();

        if( (int)$row['is_ppl'] == 1 ){
            //判断该用户是否购买过该限购产品
            $count = M('order')->alias('o')->join('__ORDER_ITEM__ item ON item.order_id = o.order_id', 'left')
                ->join('__CART__ c ON item.cart_id = c.id', 'left')
                ->where(array('o.client_id' => $_SESSION['userid'], 'goods_id' => (int)$row['id']))->count();
            if( $count > 0 ){
                //大于0表明该用户已购买过
                $row['allow_buy'] = 1;
            }else{
                $row['allow_buy'] = 0;
            }
        }

        $this->assign('photos', getImgs($pdata['imgs']));   //图片集
        $this->assign('colors', $clist);                   //商品颜色
        $this->assign('sizes', $slist);                   //商品颜色
        $this->assign('info', $info);                   //商品属性
        $this->assign('row', $row);
        $this->assign('data', $data);
        $this->display();
    }

    public function goods_info(){
        $type = I('get.type', 0);
        $data = $this->mode->field('content')->where(array('id' => I('get.id')))->find();
        switch($type){
            case 0:
                $data['name'] = '图文详情';
                break;
            case 1:
                $data['name'] = '测量方法';
                break;
            case 2:
                $data['name'] = '品牌';
                break;
            case 3:
                $data['name'] = '包装清单';
                break;
            case 4:
                $data['name'] = '售后须知';
                break;
            case 5:
                $data['name'] = '测量方法';
                break;
        }
        $data['type'] = $type;
        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 商品收藏
     */
    public function goodsCollect(){
        $collect = M('collect');
        $uid = $_SESSION['userid'];
        $goods_id = I('get.goods_id');
        $count = $collect->where(array('cid' => $uid, 'goods_id' => $goods_id))->count();
        if($count > 0){
            if( $collect->where(array('cid' => $uid, 'goods_id' => $goods_id))->delete() ){
                $this->ajaxReturn(array('status' => true, 'collect_status' => 0));
            }else{
                $this->ajaxReturn(array('status' => false));
            }
        }else{
            $getData = array(
                'cid' => $uid,
                'goods_id' => $goods_id,
                'ctime' => time()
            );
            if( $collect->add($getData) ){
                $this->ajaxReturn(array('status' => true, 'collect_status' => 1));
            }else{
                $this->ajaxReturn(array('status' => false));
            }
        }
    }

}